import req from '@/utils/request'
import { defHttp } from '/@/utils/http/axios'
import { LoginParams, LoginResultModel } from './model/userModel'
import { ErrorMessageMode } from '/#/axios'

import type {
  loginFormData,
  loginResponseDate,
  userInfoReposeData,
  pwdData,
  checkPwdRespinseData,
  userListFormData,
  userListResponseData,
  userAllResponseData,
  userInfoData,
} from '../user/type'

//项目用户相关的请求地址
enum Api {
  Login = '/login',
  UserInfo = '/user/info',
  Logout = '/logout',
  CheckPwd = '/manage/user/pwd',
  UserAll = '/manage/user/all',
  UserList = '/manage/user/list',
  UserAdd = '/manage/user/add',
  UserGetId = '/manage/user/find',
  UserUpdate = '/manage/user/update',
  UserDelete = '/manage/user/delete',
}

// 登录接口
export const reqLogin = (data: loginFormData) => req.post<any, loginResponseDate>(Api.Login, data)

/**
 * @description: 用户登录接口
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  )
}

// 获取用户信息接口
export const reqUserInfo = () => req.get<any, userInfoReposeData>(Api.UserInfo)
// 退出登录接口
export const reqLogout = () => req.post<any, any>(Api.Logout)
// 校验原密码
export const reqCheckPwd = (data: pwdData) =>
  req.post<any, checkPwdRespinseData>(Api.CheckPwd, data)
// 修改密码
export const reqUpdatePwd = (data: pwdData) =>
  req.put<any, checkPwdRespinseData>(Api.CheckPwd, data)
// 获取所有用户
export const reqUserAll = () => req.get<any, userAllResponseData>(Api.UserAll)
// 获取用户列表（分页）
export const reqUserList = (data: userListFormData) =>
  req.post<any, userListResponseData>(Api.UserList, data)
// 添加用户
export const reqUserAdd = (data: userInfoData) =>
  req.post<any, loginResponseDate>(Api.UserAdd, data)
// 根据_id查询用户信息
export const getUserById = (_id: string) =>
  req.get<any, loginResponseDate>(Api.UserGetId + `?_id=${_id}`)
// 更新用户信息
export const reqUserUpdate = (data: userInfoData) =>
  req.post<any, loginResponseDate>(Api.UserUpdate, data)
// 删除用户
export const reqUserDelete = (userId: string) =>
  req.post<any, loginResponseDate>(Api.UserDelete, { userId })
